package lxj;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/*  Arrays.sort()方法可对任何实现compareble接口的对象数组排序, 像Integer,String,这两种引用类型都实现了compareble接口，
所以这两种类型的数组都可直接使用Arrays.sort()进行排序*/
public class User4 implements Comparable<User4>{
    private int age;

    public User4(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User4{" +
                "age=" + age +
                '}';
    }

    @Override
    public int compareTo(User4 o) {
        // return o.getAge()-this.getAge()   //按年龄排序
        return  -1;     //0 1  顺序不变   -1 倒序
    }


    public static void main(String[] args) {
        List<User4> users = new ArrayList<User4>();
        users.add(new User4(26));
        users.add(new User4( 23));
        users.add(new User4( 56));
        users.add(new User4( 23));


        Collections.sort(users);

        for (User4 user : users) {
            System.out.println(user.getAge());
        }
    }
}
